/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminGroup;

import com.ControlEscolar.Beans.Group;
import com.ControlEscolar.Beans.GroupDetail;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Services.GroupService;
import com.ControlEscolar.Services.ProfessorService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author kossel
 */
public class AdminGroup extends MappingDispatchAction{
    private static final String MODIFY="m";
    ArrayList results=new ArrayList();
    ArrayList resultsDetail= new ArrayList();
    GroupService service = new GroupService();
        
      public void addGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          GroupForm groupForm = (GroupForm)form;
          Group group = new Group();
          group.setGroupName(groupForm.getGroupName());
          group.setID_Professor(groupForm.getID_Professor());
          group.setSubjectKey(groupForm.getSubjectKey());
          group.setState(groupForm.getState());
          try {
             service.createGroup(group);
          response.getWriter().print("success");
     }catch(ServiceExp ex){
             response.getWriter().print("Ya existe Un grupo con este nombre");
        }

    }

   public ActionForward viewAll (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
         resultsDetail=service.getListGroupsDetail();
         HttpSession session = request.getSession();
         session.setAttribute("groupList", resultsDetail);
        return mapping.findForward("success");
    }

        public ActionForward getGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
        String grupoName=request.getParameter("id");
         Group group = new Group();
         GroupDetail groupDetail = new GroupDetail();
         group = service.getGroup(grupoName);
         groupDetail=service.getGroupDetail(grupoName);
         GroupForm groupForm = (GroupForm)form;
         groupForm.setAction(MODIFY);
         groupForm.setGroupName(grupoName);
         groupForm.setID_Professor(group.getID_Professor());
         groupForm.setSubjectKey(group.getSubjectKey());
         groupForm.setState(group.getState());
         System.out.println(groupForm.getState());
         groupForm.setProfessorName(groupDetail.getProfessor_name());
         groupForm.setSubjectName(groupDetail.getSubjectName());
         return mapping.findForward("success");

    }

      public void modifyGroup (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          Group group = new Group();
          GroupForm groupForm = (GroupForm)form;
          group.setGroupName(groupForm.getGroupName());
          group.setID_Professor(groupForm.getID_Professor());
          group.setSubjectKey(groupForm.getSubjectKey());
          group.setState(groupForm.getState());
          groupForm.setAction("");
          try{
          service.updateState(group);
          response.getWriter().print("success");
          }catch(ServiceExp ex){
            response.getWriter().print("No se puede modificar el estado del grupo, Verifique sus datos");
          }

         }

}
